import {
	TreeExpViewActionType,
	ITreeExpViewAbility,
	ITreeExpViewController,
	ITreeExpViewStore,
	ITreeExpBarAbility,
	IParam,
	IViewActionResult,
} from '@/core/interface';
import { ExpViewController } from './exp-view-controller';

/**
 * 树导航视图控制器
 *
 * @export
 * @class TreeExpViewController
 * @extends {ExpViewController<TreeExpViewActionType, ITreeExpViewStore, ITreeExpViewAbility>}
 * @implements {ITreeExpViewController<ITreeExpViewStore, ITreeExpViewAbility>}
 */
export class TreeExpViewController
	extends ExpViewController<
		TreeExpViewActionType,
		ITreeExpViewStore,
		ITreeExpViewAbility
	>
	implements ITreeExpViewController<ITreeExpViewStore, ITreeExpViewAbility>
{
	/**
	 * 获取树导航部件能力
	 *
	 * @protected
	 * @return {*}  {(ITreeExpBarAbility | undefined)}
	 * @memberof TreeViewController
	 */
	protected getMainCtrlAbility(): ITreeExpBarAbility | undefined {
		const expbar = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'TREEEXPBAR'
		);
		return expbar
			? this.getSubAbility<ITreeExpBarAbility>(expbar.name)
			: undefined;
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [args]
	 * @return {*}  {Promise<IViewActionResult>}
	 * @memberof TreeExpViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		const expCtrl = this.getMainCtrlAbility();
		if (expCtrl) {
			return expCtrl.load();
		} else {
			return Promise.reject({
				ok: false,
				data: null,
				rowData: { status: 500 },
			});
		}
	}
}
